using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using DarkWynter.Engine.Controllers;

namespace DWControllerDemo
{
    public class ControllerDemo : Microsoft.Xna.Framework.Game
    {   
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        // Create a Singleton accessor.
        // Controller statically accesses this variable to gain visibility 
        //   into ControllerDemo local functions and variables.
        public static ControllerDemo _self;

        // Controller Manager, and User-Defined Event Arguments
        ControllerManager controllerManager;
        List<object> args = new List<object>();

        // Set up a Game Mode to switch controller types with.
        public enum GameMode { Game, Menu };
        public GameMode gameMode = GameMode.Menu;

        public Texture2D background;
        public Texture2D character;
        public Color backColor = Color.CornflowerBlue;

        public ControllerDemo()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            // Assign "this" to the singleton accessor. 
            ControllerDemo._self = this;


        }

        protected override void Initialize()
        {
            // Create a List of Child Controllers
            List<Type> controllerTypes = new List<Type>();
            controllerTypes.Add(typeof(MenuController));
            controllerTypes.Add(typeof(GameController));

            // Pass in Width and Height used by mouse.
            controllerManager = new ControllerManager(
                graphics.GraphicsDevice.Viewport.Width,
                graphics.GraphicsDevice.Viewport.Height,
                controllerTypes
                );

            // Pass "this" as EventArgs to Conroller delegates functions
            args.Add(this);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // Load some content to play with...
            background = Content.Load<Texture2D>("CreditScreen");
            character = Content.Load<Texture2D>("EleIcon");
        }

        protected override void Update(GameTime gameTime)
        {
            // Checks if any controllers Added or Removed
            controllerManager.Update();

            // Update Appropriate Controller
            if (gameMode == GameMode.Menu)
            {
                // Update all MenuControllers
                List<Controller> controllers = controllerManager.GetControllers(typeof(MenuController));
                for (int i = 0; i < controllers.Count; i++)
                {
                    controllers[i].Update(ref args);
                }
            }
            if (gameMode == GameMode.Game)
            {
                // Update all GameControllers
                List<Controller> controllers = controllerManager.GetControllers(typeof(GameController));
                for (int i = 0; i < controllers.Count; i++)
                {
                    controllers[i].Update(ref args);
                }
            }

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(backColor);



            base.Draw(gameTime);
        }

    }
}
